package com.nf.health.app.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.models.BloodSugarDataBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/31.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description
 * @email he_pann@163.com
 */
public class BloodSugarHistoryDataAdapter extends BaseAdapter {

    private Activity context;
    private BloodSugarDataBean bean;
    private ArrayList<BloodSugarDataBean.DataBean.DeviceDataBean> devicedatas;

    private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

    public BloodSugarHistoryDataAdapter(Activity context, BloodSugarDataBean bean) {
        this.context = context;
        this.bean = bean;
        devicedatas = bean.data.deviceData;
    }
    public void setNewBeanAndNotify(BloodSugarDataBean bean){
        this.bean = bean;
        notifyDataSetChanged();
        closeAllOpenedSwipeView();
    }
    @Override
    public int getCount() {
        return devicedatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        BloodSugarDataBean.DataBean.DeviceDataBean deviceDataBean = devicedatas.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_blood_sugar_history_data_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDate.setText(deviceDataBean.posttime.substring(8, 10));
        holder.tvBeforeDawn.setText(deviceDataBean.before_dawn+"");
        holder.tvBeforeGlucose.setText(deviceDataBean.before_glucose+"");
        holder.tvAfterGlucose.setText(deviceDataBean.after_glucose+"");
        holder.tvBeforeLunch.setText(deviceDataBean.before_lunch+"");
        holder.tvAfterLunch.setText(deviceDataBean.after_lunch+"");
        holder.tvAfterDinner.setText(deviceDataBean.after_dinner+"");
        holder.tvBeforeDinner.setText(deviceDataBean.before_dinner+"");
        holder.tvBeforeSleep.setText(deviceDataBean.before_sleep+"");
        //删除对应position数据监听
        holder.delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(position, "");
                closeAllOpenedSwipeView();
                unClosedSwipeView.clear();
            }
        });
        holder.swipeView.setOnSwipeStatusChangeListener(new SwipeView.OnSwipeStatusChangeListener() {

            @Override
            public void onSwiping(SwipeView swipingSwipeView) {
                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    closeAllOpenedSwipeView();
                }

                if (!unClosedSwipeView.contains(swipingSwipeView)) {
                    unClosedSwipeView.add(swipingSwipeView);
                }

            }

            @Override
            public void onOpen(SwipeView openedSwipeView) {
                for (int i = 0; i < unClosedSwipeView.size(); i++) {
                    if (unClosedSwipeView.get(i) != openedSwipeView) {
                        unClosedSwipeView.get(i).close();
                    }
                }
                if (!unClosedSwipeView.contains(openedSwipeView)) {
                    unClosedSwipeView.add(openedSwipeView);
                }
            }

            @Override
            public void onClose(SwipeView closedSwipeView) {
                unClosedSwipeView.remove(closedSwipeView);
            }
        });
        return convertView;
    }

    private OnMyClickListener listener;

    public void setOnMyClickListener(OnMyClickListener listener) {
        this.listener = listener;
    }

    private void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
    static class ViewHolder {
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_before_dawn)
        TextView tvBeforeDawn;
        @Bind(R.id.tv_before_glucose)
        TextView tvBeforeGlucose;
        @Bind(R.id.tv_after_glucose)
        TextView tvAfterGlucose;
        @Bind(R.id.tv_before_lunch)
        TextView tvBeforeLunch;
        @Bind(R.id.tv_after_lunch)
        TextView tvAfterLunch;
        @Bind(R.id.tv_before_dinner)
        TextView tvBeforeDinner;
        @Bind(R.id.tv_after_dinner)
        TextView tvAfterDinner;
        @Bind(R.id.tv_before_sleep)
        TextView tvBeforeSleep;
        @Bind(R.id.delete)
        TextView delete;
        @Bind(R.id.swipeView)
        SwipeView swipeView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
